package cn.com.b2office.struts2.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import cn.com.b2office.bean.Pagination;
import cn.com.b2office.domain.OOSReport;
import cn.com.b2office.domain.User;
import cn.com.b2office.service.OOSReportService;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @author  zhenggang
 * @version Jul 23, 2008 10:29:08 AM
 * comment
 */
public class OOSReportAction extends ActionSupport implements SessionAware,ModelDriven<Pagination>{

	private static final long serialVersionUID = -1385561314034459160L;
	
	private Map session ;
	private String itemName;
	private String comments;
	private OOSReport oosReport = new OOSReport();
	private List<OOSReport> listoosReport;
	private OOSReportService oosReportService;
	
	private Pagination pagination = new Pagination();  //分页Bean
	
	private int totalRecordNum;   //总记录数
	private int maxResults = 5;   //每页显示记录数
	
	private User user;
	
	public String execute()throws Exception{
		user = (User) session.get("user");
		if (user == null) {
			return "login";
		}
	
		oosReport.setMemberId(user.getMemberId());
		oosReport.setItemName(itemName);
		oosReport.setComments(comments);
		oosReport.setRegDate(new Date());
		oosReportService.addReport(oosReport);
		return SUCCESS;
	}
	
	public String input(){
		user = (User) session.get("user");
		if (user == null) {
			return "login";
		}
		
		totalRecordNum = oosReportService.getTotalRecordNumByMember(user.getMemberId());	
		pagination.init(totalRecordNum, maxResults);	
		listoosReport = oosReportService.find(user.getMemberId(), pagination.getFirstResult(), maxResults);
		
		return INPUT;		
	}
	
	public void  validateExecute(){
		user = (User) session.get("user");
		if(itemName==null || itemName.replace(" ", "").equals("")){
			addFieldError("itemName", "名称不能为空");
		}else if(itemName.replace(" ", "").length()>50){
			addFieldError("itemName", "商品名太长");
		}
		totalRecordNum = oosReportService.getTotalRecordNumByMember(user.getMemberId());	
		pagination.init(totalRecordNum, maxResults);	
		listoosReport = oosReportService.find(user.getMemberId(), pagination.getFirstResult(), maxResults);

	}

	public void setOosReportService(OOSReportService oosReportService) {
		this.oosReportService = oosReportService;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public List<OOSReport> getListoosReport() {
		return listoosReport;
	}

	@Override
	public Pagination getModel() {
		// 分页Bean作为返回的model
		return pagination;
	}

	public void setItemName(String itemName) {
		this.itemName = itemName;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}

	public int getTotalRecordNum() {
		return totalRecordNum;
	}

}
